﻿using System;
using System.Collections.Generic; 
using System.Text;
using WebStudio.Framework.Helper;
using System.Data;

namespace WebStudio.Support.User
{
    public class UserRule
    {
        #region 公有方法
        /// <summary>
        /// 获取信息列表
        /// </summary>
        /// <returns></returns>
        public static List<UserEntity> GetUserList()
        {
            List<UserEntity> list = new List<UserEntity>();
            using (var dr = DBHelper.ExecuteReader(string.Format("SELECT * FROM UserTB")))
            {
                return FillDate(dr);
            }
        }

        /// <summary>
        /// 根据条件获取信息列表
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public static List<UserEntity> GetUserListByCondition(string condition)
        {
            using (var dr = DBHelper.ExecuteReader(string.Format("SELECT * FROM UserTB WHERE {0}", condition)))
            {
                return FillDate(dr);
            }
        }

        /// <summary>
        /// 获取信息实体
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public static UserEntity GetUserEntity(string condition)
        {
            using (var dr = DBHelper.ExecuteReader(string.Format("SELECT * FROM UserTB WHERE {0}", condition)))
            {
                List<UserEntity> list = FillDate(dr);
                return list == null || list.Count == 0 ? (new UserEntity()) : list[0];
            }
        }

        /// <summary>
        /// 根据标识获取信息实体重载一
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public static UserEntity GetUserEntity(int userId)
        {
            return GetUserEntity(string.Format("SELECT * FROM UserTB WHERE UserId = {0}", userId));
        }

        /// <summary>
        /// 检查是否已经存在
        /// </summary>
        /// <param name="teamName"></param>
        /// <returns></returns>
        public static bool CheckUserIsExisted(string userName)
        {
            using (var dr = DBHelper.ExecuteReader(string.Format("SELECT * FROM UserTB WHERE UserName = '{0}'", userName)))
            {
                return (dr != null && dr.Read());
            }
        }

        /// <summary>
        /// 保存信息
        /// </summary>
        /// <param name="te"></param>
        /// <returns></returns>
        public static bool SaveUserInfor(UserEntity te)
        {
            int i = 0;
            if (te.UserID <= 0)
            {
                i = DBHelper.ExecuteNonQuery(string.Format("INSERT INTO UserTB (UserName,Password,Email,IsAdmin,Sex,Description) VALUES('{0}','{1}','{2}',{3},'{4}','{5}')", te.UserName, te.Password, te.Email, te.IsAdmin, te.Sex, te.Description));
            }
            else
            {
                i = DBHelper.ExecuteNonQuery(string.Format("UPDATE UserTB SET UserName ='{0}' ,Password ='{1}',Email={2},IsAdmin={3},Sex='{4}',Description={5} WHERE UserID = {6}", te.UserName, te.Password, te.Email, te.IsAdmin, te.Sex, te.Description, te.UserID));
            }
            return i > 0 ? true : false;
        }

        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool DeleteUserInfor(string userId)
        {
            return DBHelper.ExecuteNonQuery(string.Format("DELETE FROM UserTB WHERE UserID = {0}", userId)) >= 0 ? true : false;
        }
        #endregion

        #region 私有方法
        /// <summary>
        /// 填充数据
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        private static List<UserEntity> FillDate(IDataReader dr)
        {
            List<UserEntity> list = new List<UserEntity>();
            UserEntity te;
            while (dr != null && dr.Read())
            {
                te = new UserEntity();
                te.UserID = ConvertHelper.IntParse(dr["UserID"]);
                te.UserName = ConvertHelper.StringParse(dr["UserName"]);
                te.Password = ConvertHelper.StringParse(dr["Password"]);
                te.IsAdmin = ConvertHelper.IntParse(dr["IsAdmin"]);
                te.Sex = ConvertHelper.StringParse(dr["Sex"]); 
                te.Email = ConvertHelper.StringParse(dr["Email"]);
                te.Description = ConvertHelper.StringParse(dr["Description"]);

                list.Add(te);
            }
            return list;
        }
        #endregion

    }
}
